/* ZouJL ================================================================= */
$(function() {

    var $label_checked = $('.type-check .label'),
        $label_box = $('.type-check .label'),
        $labels = $label_box.find('a'),
        $check_result = $('.check-result'),
        $games = $('.game-box'),
        $highlights = {} // 当前显示的game-box
        ,
        types = {};
    /*types{
            'wanfa': ''
            , 'leixing': ''
            , 'ticai': ''
        }*/

    $labels.bind('click', function() {
        var tag = $(this).attr('href'),
            typeTag = $(this).parent().attr('data-tag');

        if ($(this).hasClass('checked')) {
            $(this).removeClass('checked');
            types[typeTag] = '';
            if (!$label_checked.find('a.checked').length) {
                checkGames('all');
            }else{
              checkGames(types);  
            }
            return false;

        } else {
            var $checked = $(this).siblings('.checked').eq(0);
            types[typeTag] = tag;
            if ($checked.length) {
                $checked.removeClass('checked');
            }
            $(this).addClass('checked');
            checkGames(types);
            return false;
        }

    });

    $label_checked.delegate('a', 'click', function() {

        if( $(this).hasClass('checked') ) return false;

        var href = $(this).attr('data-tag'),
            tag = $(this).attr('href');

        $(this).remove();

        $label_box.filter('[data-tag=' + href + ']').find('[href=' + tag + ']').removeClass('checked');

        types[href] = '';

        if (!$label_checked.find('a.checked').length) {
            checkGames('all');
        } else {
            checkGames(types);
        }

        return false;

    }).find('a.initChecked').trigger('click');


    function checkGames(types) {
        if (!types || typeof types === 'string') {
            $games.addClass("visible");
            $games.each(function(i) {
                if ($games.length - 1 <= i) {
                    $(this).fadeIn(function() {
                        filmType();
                    });
                    return;
                }
                $(this).fadeIn();
            });
            return;
        }

        var ta = types['leixing'] || '',
            tb = types['shangying'] || '',
            tc = types['diyu'] || '';
			

        $games.removeClass('visible');

        $highlights = $games
//            .filter('[data-typea*=' + ta + ']')
            .filter('[data-typeb*=' + tb + ']')
//            .filter('[data-typec*=' + tc + ']')
            .addClass('visible');
			

        var $others = $games.not('.visible');

        var len = $highlights.length;

        if (len) {
            $games.fadeOut(200);
            $highlights.each(function(i) {
                if ($highlights.length - 1 <= i) {
                    $(this).fadeIn(function() {
                        filmType();
                    });
                    return;
                }
                $(this).fadeIn();
            });
        } else {
            $games.fadeOut(200);            
        }
        $check_result.find('strong').text(len).end().show();
        filmType();
    }



});

/* ZouJL ============================== END ========================== */